package com.example.shop_car_2208.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_car_2208.MainActivity
import com.example.shop_car_2208.MainActivity2
import com.example.shop_car_2208.R
import com.example.shop_car_2208.databinding.ActivityLofinUserBinding
import com.example.shop_car_2208.db.MyDBUtils
import com.example.shop_car_2208.mvvm.BaseActivity
import com.example.shop_car_2208.register.RegisterActivity
import kotlinx.coroutines.launch

/**
 * @Route 路由注解：定义跳转页面的路劲名称 名称规范，/***/*** 同一个App下前方名称一致 不同App的前方名称不能相同
 */
@Route(path = "/app/login")
class LofinUserActivity : BaseActivity<ActivityLofinUserBinding, LoginViewModel>() {
    override fun initData() {
        viewModel.loginSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code == 200){
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("id",it.data.id)//方便保存用户id 方便后续在我的界面里进行查询显示
                //在登录成功时将登录地址保存,方便在订单界面展示和修改
                SPUtils.getInstance().put("address",it.data.address)
               // MyDBUtils.getDB().getUserD
                // ao().insertUser(it.data)
                lifecycleScope.launch {
                    MyDBUtils.getDB().getUserDao().insertUser(it.data)
                }
//                ARouter.getInstance().build("/app/MainActivity").withString("name",it.data.username).navigation()
//                ARouter.getInstance().build("/app/MainActivity").withString("pwd",it.data.password).navigation()
                ARouter.getInstance().build("/app/MainActivity").withSerializable("user", it.data).navigation()


            }
        }
    }

    override fun initView() {
        binding.btn.setOnClickListener {
            val toString = binding.et1.text.toString()
            val toString1 = binding.et2.text.toString()
//            if (toString.isEmpty()||toString1.isEmpty()){
//                ToastUtils.showLong("用户名，密码不能为空")
//                return@setOnClickListener
//            }
//            if (!RegexUtils.isMobileSimple(toString)){
//                ToastUtils.showLong("用户名的格式不正确，应该为手机号")
//            }else if (!RegexUtils.isMatch("^[\\w]{6,8}\$",toString1)){
//                ToastUtils.showLong("，密码格式不正确，应该为6-8位的数字，字母，下划线组合")
//            }
            viewModel.Login(binding.et1.text.toString(),binding.et2.text.toString())

        }
        binding.text.setOnClickListener {
//            val intent = Intent(this,RegisterActivity::class.java)
//            startActivityForResult(intent,101)
            ARouter.getInstance().build("/app/register").navigation(this,101)

        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayout(): Int = R.layout.activity_lofin_user
//接受注册成功后的返回值
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 101&&resultCode == 102){
            val username = data?.extras?.getString("username")
            val password = data?.extras?.getString("password")
            binding.et1.setText(username)
            binding.et2.setText(password)
        }
    }
}